package day13.CollectionTask;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class TaskThree {
    public static void main(String[] args) {
        List<Commodity> list=new ArrayList<>();
        list.add(new Commodity(1,"手机",2000.00));
        list.add(new Commodity(2,"电脑",6000.00));
        list.add(new Commodity(3,"平板",3000.00));
        //list可根据引用类型的某个类型排序
 //       list.sort(Comparator.comparingDouble(Commodity::getPrice).reversed());//default void sort(Comparator<? super E> c) {
//        Collections.sort( list,Comparator.comparingDouble(Commodity::getPrice).reversed());// public static <T> void sort(List<T> list, Comparator<? super T> c) {
    list.sort(new Comparator<Commodity>(){

        @Override
        public int compare(Commodity o1, Commodity o2) {
            return (int)(o2.getPrice()-o1.getPrice());
        }
    });
        for(Commodity commodity:list){
        System.out.println(commodity);
    }
    }
}
class Commodity{
    private int ID;
    private String name;
    private Double price;
    public Commodity(int ID, String name, Double price) {
        this.ID = ID;
        this.name = name;
        this.price = price;
    }
public Commodity(){
}
    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Commodity{" +
                "ID=" + ID +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
